<?php
namespace app\index\controller;
use EasyWeChat\Factory;
use app\index\model;
header("Access-Control-Allow-Origin:*");
header('Access-Control-Allow-Headers:x-requested-with,content-type');
class Base extends \app\common\controller\Base {
    public function __construct() {
        parent::__construct();
    }
    public function uploadpic () {
        require_once  VENDOR_PATH.'qiniu/autoload.php';
         // 用于签名的公钥和私钥
        $accessKey = config('qiniu_accesskey');
        $secretKey = config('qiniu_secretkey');
        $bucket = config('qiniu_bucket');
        $domain = config('qiniu_domain');
        $file = request()->file('file');
        // 要上传图片的本地路径
        $filePath = $file->getRealPath();
        $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);
        // 上传到七牛后保存的文件名
        $key =substr(md5($file->getRealPath()) , 0, 5). date('YmdHis') . rand(0, 9999) . '.' . $ext;
        // 需要填写你的 Access Key 和 Secret Key
        // 构建鉴权对象
        $auth = new \Qiniu\Auth($accessKey, $secretKey);
        // 要上传的空间
        $token = $auth->uploadToken($bucket);
        // 初始化 UploadManager 对象并进行文件的上传
        $uploadMgr = new \Qiniu\Storage\UploadManager();
        // 调用 UploadManager 的 putFile 方法进行文件的上传
        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
        if ($err !== null) {
            var_dump($err);
        } else {
            return returnJson(0, '上传成功',$domain.$ret['key']);
        }
    }
}
